#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wx

# This file is part of Beremiz, a Integrated Development Environment for
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
#
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
#
# See COPYING file for copyrights details.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
from board.board_root import boards
from ..toolchain_platformio import toolchain_platformio

_ = wx.GetTranslation


class Win32_pio_target(toolchain_platformio):
    def __init__(self, CTRInstance):
        toolchain_platformio.__init__(self, CTRInstance)
        self.dlopen_prefix = ""
        self.extension = ".dll"
        self.platform = 'https://gitee.com/kaikong/platform-windows_amd64.git'
        self.framework = ''
        self.board = ''
        self.build_flags = ['-Dwindows_x86', '-DPLC_WIN', '-DHWMODEL=\\"Windows\\"', '-DONLINE_DEBUG']
        self.exe = 'program.dll'
        self.static = False
        self.libs = ['plc_app']
        self.host = None

    def getBoard(self, model=None):
        if model is None:
            boardNode = self.CTRInstance.GetChildByName('board_0')
            if not boardNode:
                self.CTRInstance.logger.error(_('Can not Found "board_0",Please attach one board.'))
                return False
            board = boardNode.board_class
            self.rte = boardNode.current_rte
            self.app = boardNode.current_plc
        else:
            board = boards[model].board()
            self.rte = board.rte()
            self.app = board.plc()
        self.model = board.model
        # self.board = 'board = %s' % board.board
        self.upload_protocol = board.upload_protocol
        self.framework = ""
        if not self.rte:
            raise Exception(_('Please select one Firmware.'))
        self.build_flags += board.LDFLAGS + self.app.LDFLAGS or []
        self.libs = self.app.lib or []
        self.deps = self.app.deps or []
        self.lib_other = board.lib_other or []
        self.options = self.app.options or []
        if self.rte.bin:
            self.mode = 'APP'
        else:
            self.mode = 'PLC'
        if self.app.framework:
            self.framework = 'framework = %s' % self.app.framework

    def getPlatform(self):
        return self.platform

    # async def debug_start(self):
    #     query = '''
    #     query{
    #     local_plc(start:true)
    #     }
    #     '''
        # res = await localClient.execute_async(query=query)
        # return res['data']['local_plc']
